package org.uim.cricket.storage.queries;

import org.uim.cricket.entities.aggr.SingleInning;
import org.uim.cricket.entities.inning.InningExtras;
import org.uim.cricket.entities.inning.InningTotals;
import org.uim.cricket.entities.match.CricketMatch;
import org.uim.cricket.entities.partnerships.FallOfWicket;
import org.uim.cricket.entities.partnerships.Partnership;

public class SingleInningQueryGenerator extends QueryGenerator {

	private SingleInning inning = null;
	
	public SingleInningQueryGenerator(CricketMatch theMatch, SingleInning inn) {
		super(theMatch);
		this.inning = inn;
	}

	@Override
	public String generateQuery() throws Exception {
		StringBuilder query = new StringBuilder("CALL sp_Add_MatchInning(");
		
		String id = this.getMatch().getId().getPrefix() + "#" + this.getMatch().getId().getInternationalID();
		
		this.appendQuery(query, id, 12, true);
		this.appendQuery(query, inning.getInningMatch());
		this.appendQuery(query, inning.getBattedTeam(), 128, false);
		this.appendQuery(query, inning.getBowledTeam(), 128, false);
		
		InningTotals total = inning.getTotals();
		
		this.appendQuery(query, total.getRuns());
		this.appendQuery(query, total.getOvers(), true);
		this.appendQuery(query, total.getWickets());
		this.appendQuery(query, total.getMinutes());
		query.append(total.isDeclared()).append(",");
		
		InningExtras extras = inning.getExtras();
		this.appendQuery(query, extras.getLegBies());
		this.appendQuery(query, extras.getBies());
		this.appendQuery(query, extras.getNoBalls());
		this.appendQuery(query, extras.getWides());
		this.appendQuery(query, extras.getPanelties());
		this.appendQuery(query, extras.getOthers(), false);
		
		query.append(");\n");
		
		if (this.inning.getTargetChased() > 0) {
			query.append("CALL sp_Add_InningChasing(");
			this.appendQuery(query, id, 12, true);
			this.appendQuery(query, inning.getInningMatch());
			this.appendQuery(query, inning.getTargetChased());
			this.appendQuery(query, inning.getTargetChasedOvers(), false);
			query.append(");\n");
		}
		
		if (this.inning.getFallWickets() != null) {
			for (int f=0; f<this.inning.getFallWickets().size(); f++) {
				FallOfWicket fow = this.inning.getFallWickets().get(f);
				if (fow == null) continue;
				
				query.append("CALL sp_Add_FallOfWicket(");
				this.appendQuery(query, id, 12, false);
				this.appendQuery(query, inning.getInningMatch());
				this.appendQuery(query, fow.getWicket());
				if (fow.getFallOfOver() != null) 
					this.appendQuery(query, fow.getFallOfOver(), true);
				else 
					query.append("NULL,");
				
				this.appendQuery(query, fow.getFallOfRuns());
				if (fow.getOutPlayer() != null) 
					query.append(fow.getOutPlayer().getId()).append(",");
				else 
					query.append("NULL,");
				
				query.append(fow.isRetiredHurt()).append(");\n");
			}
		}
		
		if (this.inning.getPartnerships() != null) {
			
			for (int p=0; p<this.inning.getPartnerships().length; p++) {
				Partnership ps = this.inning.getPartnerships()[p];
				if (ps == null) continue;
				
				query.append("CALL sp_Add_Partnership(");
				this.appendQuery(query, id, 12, false);
				this.appendQuery(query, inning.getInningMatch());
				this.appendQuery(query, ps.getWicketNo());
				this.appendQuery(query, ps.getRuns());
				if (ps.getBalls() <= 0) {
					query.append("0,");
				} else {
					this.appendQuery(query, ps.getBalls());
				}
				if (ps.getStartPlayer() != null) {
					query.append(ps.getStartPlayer().getBatsman().getId()).append(",");
				} else {
					query.append("NULL,");
				}
				if (ps.getArrivedPlayer() != null) {
					query.append(ps.getArrivedPlayer().getBatsman().getId()).append(",");
				} else {
					query.append("NULL,");
				}
				
				if (ps.getOutPlayer() == null || ps.getOutPlayer().getBatsman() == null) {
					query.append("NULL,");
				} else {
					query.append(ps.getOutPlayer().getBatsman().getId()).append(",");
				}
				query.append(ps.isNotOut()).append(",");
				query.append(ps.isRetired()).append(");\n");
			}
			
		}
		
		return query.toString();
	}

	public void setInning(SingleInning inning) {
		this.inning = inning;
	}
	
	public SingleInning getInning() {
		return inning;
	}
	
}
